package com.epoint.service;

import java.util.List;
import java.util.UUID;

import com.epoint.dao.BookInfoDao;
import com.epoint.dao.BorrowInfoDao;
import com.epoint.entity.BookInfo;

/**
 * @Description
 * @author ylc
 */
public class BookInfoService {
	BookInfoDao bookInfoDao = new BookInfoDao();
	BorrowInfoDao borrowInfoDao = new BorrowInfoDao();

	/**
	 * 图书列表展示
	 * 
	 * @param bookName
	 * @param bookType
	 * @param pageIndex
	 * @param pageSize
	 * @param sortField
	 * @param sortOrder
	 * @return
	 */
	public List<BookInfo> getBookList(String bookName, String bookType, int pageIndex, int pageSize, String sortField,
			String sortOrder) {
		return bookInfoDao.getBookList(bookName, bookType, pageIndex, pageSize, sortField, sortOrder);
	}

	/**
	 * 图书总数
	 * 
	 * @param bookName
	 * @param bookType
	 * @return
	 */
	public int getBookListCount(String bookName, String bookType) {
		return bookInfoDao.getBookListCount(bookName, bookType);
	}

	/**
	 * 查询所有图书名称
	 * 
	 * @return
	 */
	public List<BookInfo> getAll() {
		return bookInfoDao.getAll();
	}

	/**
	 * 添加图书信息
	 * 
	 * @param bookInfo
	 * @return
	 */
	public int addBookInfo(BookInfo bookInfo) {
		// 生成UUID编号
		bookInfo.setBookId(UUID.randomUUID().toString());
		// 同一位作者的书名不能重复,其余数据必填
		String author = bookInfo.getAuthor();
		String bookname = bookInfo.getBookName();
		String bookId = bookInfoDao.getBookIdByAuthorAndBookName(author, bookname);
		if ("".equals(bookId)) {
			return bookInfoDao.addBookInfo(bookInfo);
		} else {
			// 处理重名验证
			return 2;
		}
	}

	/**
	 * 根据bookId查询图书信息
	 * 
	 * @param bookId
	 * @return
	 */
	public BookInfo findById(String bookId) {
		return bookInfoDao.findById(bookId);
	}

	/**
	 * 修改图书信息
	 * 
	 * @param bookInfo
	 * @return
	 */
	public int updateBookInfo(BookInfo bookInfo) {
		// 同一位作者的书名不能重复,其余数据必填
		String author = bookInfo.getAuthor();
		String bookname = bookInfo.getBookName();
		String bookId = bookInfoDao.getBookIdByAuthorAndBookName(author, bookname);
		if ("".equals(bookId) || bookId.equals(bookInfo.getBookId())) {
			return bookInfoDao.updateBookInfo(bookInfo);
		} else {
			return 2;
		}
	}

	/**
	 * 删除图书信息
	 * 
	 * @param bookId
	 * @return
	 */
	public int deleteBookInfoByBookId(String bookId) {
		// 当前图书被借阅时不能删除
//		int res = borrowInfoDao.isBorrowByBookId(bookId);
//		if (res > 0) {
//			return 3;
//		} else {
//			
//		}
		return bookInfoDao.deleteBookInfoByBookId(bookId);
	}
}
